// pages/user/erji//myCoupon/myCoupon.js
var base = require('../../../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数
const PAGES = require('../../../../utils/pages.js')
const { HTTP } = require('../../../../lib/http.js')
const { URL } = require('../../../../lib/urls.js')
const { getUserToken, getUserInfo } = require('../../../../lib/user.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: [],
    dataList: [],
    isFromSearch: true, // 用于判断searchSongList数组是不是空数组，默认true，空的数组
    searchPageNum: 1,   // 设置加载的第几次，默认是第一次
    callbackcount: 6,   //返回数据的个数
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏
    searchLoadingComplete: false,  //“没有数据”的变量，默认false，隐藏
    isShowNullView: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _t: base._t(), //翻译
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.fetchDataList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      searchPageNum: 1,  //每次触发上拉事件，把searchPageNum+1
      isFromSearch: true  //触发到上拉事件，把isFromSearch设为为false
    });
    that.fetchDataList();
  },
  //滚动到底部触发事件
  onReachBottom: function () {
    let that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1,  //每次触发上拉事件，把searchPageNum+1
        isFromSearch: false  //触发到上拉事件，把isFromSearch设为为false
      });
      that.fetchDataList();
    }
  },
  //获取优惠券列表
  fetchDataList: function () {
    wx.showLoading({
      title: '加载中...'
    })
    let that = this;
    let searchPageNum = that.data.searchPageNum,//把第几次加载次数作为参数
      callbackcount = that.data.callbackcount; //返回数据的个数
    HTTP({
      url: URL.user.couponList,
      userToken: getUserToken(),
      data: {
        PageIndex: searchPageNum,
        PageSize: callbackcount,
      },
      success: (ress) => {
        wx.stopPullDownRefresh();
        wx.hideLoading();
        var res = ress.Data;
        if (res.CouponInfos.length > 0) {
          console.log("hlw:" + res)
          let searchList = [];
          var list = [];
          for (var i = 0; i < res.CouponInfos.length; i++) {
            var model = res.CouponInfos[i];
            if (model) {
              model.isGoodsQuan = model.MainImg.length>0?1:0;
              list.push(model);
            }
          }
          //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加
          this.data.isFromSearch ? searchList = list : searchList = that.data.dataList.concat(list)
          this.setData({
            dataList: searchList, //获取数据数组
            searchLoading: true,   //把"上拉加载"的变量设为false，显示
            isShowNullView: searchList.length > 0 ? false : true
          });
          if (res.CouponInfos.length < callbackcount) {
            //数据已加载完毕
            that.setData({
              searchLoadingComplete: true, //把“没有数据”设为true，显示
              searchLoading: false  //把"上拉加载"的变量设为false，隐藏
            });
          }
          //没有数据了，把“没有数据”显示，把“上拉加载”隐藏
        } else {
          if (that.data.searchPageNum == 1) {
            that.setData({
              dataList: [],
              isShowNullView: true
            })
          }
          that.setData({
            searchLoadingComplete: true, //把“没有数据”设为true，显示
            searchLoading: false  //把"上拉加载"的变量设为false，隐藏
          });
        }
      }
    })
  },
  useCouponTap: function (e) {
    let index = e.currentTarget.dataset.index;//
    var item = this.data.dataList[index];
    let data = JSON.stringify(item);
    wx.navigateTo({
      url: PAGES.couponUse + '?data=' + encodeURIComponent(data)
    })
  },
})